package com.jt.aspect;

import com.jt.anno.UseRedis;
import com.jt.util.ObjectMapperUtil;
import com.jt.vo.EasyUITree;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * @author 张琪
 * @date 2020/12/8
 * @describe
 */

public class doCache {

   /* @Autowired
    private ShardedJedis jedis;

    @Pointcut("@annotation(com.jt.anno.UseRedis)")
    public void useRedis(){}


//    @Around("useRedis()")
    public Object doRedis(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs(); //  获取方法的参数
        Object data;
//        获取目标方法的对象类型, 基于此类型获取目标方法
        Class<?> targetClass = joinPoint.getTarget().getClass();
//        获取方法签名,存储了方法信息的一个对象
        MethodSignature ms =(MethodSignature) joinPoint.getSignature();
//        获取目标方法
        Method targetMethod = targetClass.getDeclaredMethod(ms.getName(), ms.getParameterTypes());
//        获取目标方法上的注解对象
        UseRedis useRedis = targetMethod.getAnnotation(UseRedis.class);
        String key = useRedis.key() + args[0];
        System.out.println(key);
        long t1, t2;
        if(jedis.exists(key)) {
            t1 = System.currentTimeMillis();
            String json = jedis.get(key);
            data = ObjectMapperUtil.toObject(json, targetMethod.getClass());
            t2 = System.currentTimeMillis();
            System.out.println("redis耗时: " + (t2 - t1));
        } else {
            t1 = System.currentTimeMillis();
            data = joinPoint.proceed();
            jedis.set(key, ObjectMapperUtil.toJson(data));
            t2 = System.currentTimeMillis();
            System.out.println("数据库耗时: " + (t2 - t1));
        }

        return data;
    }*/
}
